<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="85px">
      <el-form-item label="居民姓名" prop="elderName" >
        <el-input
          v-model="queryParams.elderName"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 150px"
        />
      </el-form-item>
      <el-form-item label="探访者姓名" prop="visitorName" >
        <el-input
          v-model="queryParams.visitorName"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 150px"
        />
      </el-form-item>
      <el-form-item label="探访时间" prop="visitTime">
        <el-date-picker
          v-model="queryParams.time"
          type="datetimerange"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:visitRecords:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['business:visitRecords:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-table v-loading="loading" :data="visitRecordsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="探访者姓名" align="center" prop="visitorName" />
      <el-table-column label="居民姓名" align="center" prop="elderName" />
      <el-table-column label="公寓名称" align="center" prop="apartmentName" :width="150"/>
      <el-table-column label="探访时间" align="center" prop="visitTime" :width="200" ></el-table-column>
      <el-table-column label="老人身体状况" align="center" prop="physicalCondition" />
      <el-table-column label="饮食情况" align="center" prop="dietStatus" />
      <el-table-column label="活动情况" align="center" prop="activityStatus" />
      <el-table-column label="其他" align="center" prop="otherObservations" />
      <el-table-column label="备注" align="center" prop="notes" />
      <el-table-column label="建议" align="center" prop="suggestions" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:visitRecords:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:visitRecords:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改探访记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="探访人" prop="visitorName">
          <el-input v-model="form.visitorName" placeholder="" />
        </el-form-item>
        <el-form-item label="居民姓名" prop="elderName">
          <el-select v-model="form.elderId" filterable placeholder="请选择" @change="handleElderChange(form.elderId)">
            <el-option
              v-for="item in elderList"
              :key="item.elderId"
              :label="item.elderName"
              :value="item.elderId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公寓名称" prop="apartmentName">
          <el-select v-model="form.apartmentId" disabled  placeholder="请选择" :disabled="!form.elderId">
            <el-option
              v-for="item in elderList"
              :key="item.elderId"
              :label="item.apartmentName"
              :value="item.apartmentId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="探访时间" prop="visitTime">
          <el-date-picker
            v-model="form.visitTime"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="身体状况描述" prop="physicalCondition">
          <el-input v-model="form.physicalCondition" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="饮食情况" prop="dietStatus">
          <el-input v-model="form.dietStatus" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="活动情况" prop="activityStatus">
          <el-input v-model="form.activityStatus" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="其他" prop="otherObservations">
          <el-input v-model="form.otherObservations" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input v-model="form.notes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="建议" prop="suggestions">
          <el-input v-model="form.suggestions" placeholder="请输入建议" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVisitRecords, getVisitRecords, delVisitRecords, addVisitRecords, updateVisitRecords } from "@/api/business/visitRecords";
import {getElderApartment, listAllElderInfo, listElderInfo} from "@/api/business/elderInfo";
import moment from 'moment';
import {parseTime} from "@/utils/ruoyi";


export default {
  name: "VisitRecords",
  data() {
    return {
      //探访日期
      vistTime:[],
      //用户信息
      user:{},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 探访记录表格数据
      visitRecordsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        apartmentName: null,
        elderName: null,
        visitorName: null,
        startTime:null,
        endTime:null,
        time: [],
        visitTime: null,
        visitDate: null,
      },
      // 表单参数
      form: {
        apartmentId: "",
      },
      // 表单校验
      rules: {
      },
      apartmentList:{},
      elderList:{},
    };
  },
  created() {
    this.getList();
    this.getApartment();
    this.getElderList();
    //获取用户信息
    this.user = this.$store.state.user;
  },
  computed: {
    // formattedCheckInTime: {
    //   get() {
    //     return this.form.visitTime ? new Date(this.form.visitTime) : null;
    //   },
    //   set(value) {
    //     this.form.visitTime = value ? value.getTime() : null;
    //   },
    // },
  },
  methods: {
    /** 查询探访记录列表 */
    getList() {
      this.loading = true;
      console.log(this.queryParams);
      if(this.queryParams.time != null) {
        const time = this.queryParams.time;
        this.queryParams.startTime = time[0];
        this.queryParams.endTime = time[1];
      }
      listVisitRecords(this.queryParams).then(response => {
        this.visitRecordsList = response.rows;
        this.visitRecordsList.forEach(item =>{
          if(item.visitTime){
            item.visitTime = parseTime(item.visitTime);
          }
        })

        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        elderId: null,
        apartmentId: null,
        visitDate: null,
        apartmentName: null,
        elderName: null,
        visitorName: null,
        visitTime: null,
        physicalCondition: null,
        mentalState: null,
        dietStatus: null,
        activityStatus: null,
        socialInteraction: null,
        specialNeeds: null,
        otherObservations: null,
        notes: null,
        suggestions: null,
        followUpItems: null,
        createUserId: null,
        createTime: null,
        updateUserId: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;

      console.log(this.queryParams)
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加探访记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getVisitRecords(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改探访记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.updateUserId = this.user.id;
            if(!this.form.vistTime)
              this.form.visitTime = this.form.visitTime.getTime().valueOf();
            console.log(this.form.vistTime);
            updateVisitRecords(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.createUserId = this.user.id;
            if(!this.form.vistTime)
              this.form.visitTime = this.form.visitTime.getTime().valueOf();
            addVisitRecords(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除探访记录编号为"' + ids + '"的数据项？').then(function() {
        return delVisitRecords(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/visitRecords/export', {
        ...this.queryParams
      }, `visitRecords_${new Date().getTime()}.xlsx`)
    },
    getApartment(){
      getElderApartment().then(response =>{
        this.apartmentList = response.rows;
        console.log(this.apartmentList);
      })
    },
    /** 查询老人信息列表 */
    getElderList() {
      listAllElderInfo().then(response => {
        this.elderList = response.rows;
      });
    },
    handleElderChange(elderId) {
      // 在选择居民姓名的 el-select 变化时触发
      // 在这里你可以根据需要执行一些逻辑
      this.form.apartmentId = this.elderList.find(item => item.elderId === elderId).apartmentId;
      console.log(this.form.apartmentId);
    },
    test(){
      console.log(this.vistTime);
    }
  }
};
</script>
